package com.sc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.mapper.PostcommentsMapper;
import com.sc.pojo.Postcomments;
import com.sc.service.PostcommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PostcommentsServiceImpl implements PostcommentsService {
    @Autowired
    PostcommentsMapper mapper;

    @Override
    public int add(Postcomments pc) {
        //该帖子评论数加1
        mapper.addPostComments(pc.getPostid());
        //帖子_评论人表加一条数据
        mapper.addUserComment(pc.getPostid(),pc.getCommenterid());
        //评论表加一条数据
        return mapper.insertSelective(pc);
    }

    @Override
    public PageInfo<Postcomments> show(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Postcomments> list=mapper.show();
        PageInfo<Postcomments> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public PageInfo<Postcomments> search(Integer pageNum, Integer pageSize, String str) {
        PageHelper.startPage(pageNum,pageSize);
        List<Postcomments> list=mapper.search(str);
        PageInfo<Postcomments> p=new PageInfo<>(list);
        return p;
    }

    @Override
    public int del(Integer id) {
        Postcomments pc=mapper.selectByPrimaryKey(id);
        //该帖子评论数减1
        mapper.subPostComments(pc.getPostid());
        //帖子_评论人表删除一条数据
        mapper.subUserComment(pc.getPostid(),pc.getCommenterid());
        //评论表删除一条数据
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int delAll(Integer[] ids) {
        int i=0;
        for (Integer id:ids){
            Postcomments pc=mapper.selectByPrimaryKey(id);
            //该帖子评论数减1
            mapper.subPostComments(pc.getPostid());
            //帖子_评论人表删除一条数据
            mapper.subUserComment(pc.getPostid(),pc.getCommenterid());
            //评论表删除一条数据
            i+= mapper.deleteByPrimaryKey(id);
        }
        return i;
    }
}
